package com.erdos.common.util.itext;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;

public class XmlItextConfigUtil {
	private final static Log logger=LogFactory.getLog(XmlItextConfigUtil.class);
	private  static Document doc;
	
	@SuppressWarnings("unchecked")
	public static List<Node> getElements(String xPath, String configName){
		loadXml(configName);
		//logger.debug("doc:"+doc.asXML());
		List<Node> result = new ArrayList<Node>();
		//logger.debug("xPath:"+xPath);
		logger.debug("doc.selectNodes(xPath):"+doc.selectNodes("//template"));
		List<Node> elems =doc.selectNodes(xPath);
		if (elems != null)
			for (int i = 0; i < elems.size(); i++) {
				Node e = elems.get(i);
				result.add(e);
			}
		return result;
	}
	
	private static void loadXml(String configName){
		try {
			ClassPathResource resource = new ClassPathResource(configName);
			SAXReader reader = new SAXReader();
			doc = reader.read(resource.getInputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
